blue = Color.new(0,0,255)
green = Color.new(0,255,0)
white = Color.new(255,255,255)
red = Color.new(255,0,0)

counter = Timer.new()
counter:start()

wallpaper = Image.load("bg.png")

math.randomseed(os.time()) --/// prevent same random numbers from occuring each run of the game///

player = Image.createEmpty(32,32)
Player = { width=player:width(), height=player:height(), image = player } --/// players position and image info///
Player.x = 300
Player.y = 240

Player.image:clear(blue) -- clear players image to color blue


enemy = Image.createEmpty(32,32)
Enemy = {width=enemy:width(), height=enemy:height(), image = enemy } --///enemy's info///
Enemy.x = 50
Enemy.y = 50

Enemy.image:clear(green) --/// clear enemy image to green///

function movePlayer() --/// function to move our player when directionals are pressed///
pad = Controls.read()
if pad:left() then
Player.x = Player.x - 4
end
if pad:right() then
Player.x = Player.x + 4
end
if pad:up() then
Player.y = Player.y  - 4
end
if pad:down() then
Player.y = Player.y + 4
end
if pad:triangle() then
dofile("gameover.lua")
end
end

function chasePlayer() --/// function to make the enemy chase the player///
stallchase = math.random(2) --/// stallchase variable set to random number. this variable prevents the enemy from being too perfect while chasing you.///
if stallchase == 1 then --/// if stallchase resulted to 1 then move x value towards players x value///
if Enemy.x > Player.x then
Enemy.x = Enemy.x - 4
elseif Enemy.x < Player.x then
Enemy.x = Enemy.x + 4
end
end
stallchase = math.random(2)
if stallchase == 1 then --/// if stallchase resulted to 1 then move y value towards players y value///
if Enemy.y > Player.y then
Enemy.y = Enemy.y - 4
elseif Enemy.y < Player.y then
Enemy.y = Enemy.y + 4
end
end
end

function collisionCheck(object, object2)
if (object2.x + object2.width > object.x) and (object2.x < object.x + object.width) and (object2.y + object2.height > object.y) and (object2.y < object.y + object.height) then
return 1
end
end				 -- end function



while true do
screen:clear()
currentTime = counter:time()
if currentTime < 2000 then
paused = true
elseif currentTime > 2500 then
paused = false
movePlayer() --///call movePlayer function///

chasePlayer() --///call chasePlayer function////
end
if paused == true and currentTime < 2000 then
screen:print(200,100,"ready",white)
end
if paused == true and currentTime > 2000 and currentTime < 2500 then
screen:print(200,100,"Go!!!",white)
end
screen.waitVblankStart()
pad = Controls.read()
if collisionCheck(Player, Enemy) or
collisionCheck(Player, Enemy) or
collisionCheck(Player, Enemy) then
counter:stop()
end



screen:clear()



--/// paste player and enemy to screen///
screen:blit(0, 0, wallpaper, false)
screen:blit(Player.x,Player.y,Player.image)
screen:blit(Enemy.x,Enemy.y,Enemy.image)

if collisionCheck(Player, Enemy) or
collisionCheck(Player, Enemy) or
collisionCheck(Player, Enemy) then

movePlayer() --///call movePlayer function///

chasePlayer() --///call chasePlayer function////
end



screen.waitVblankStart()
screen.flip()
end